package com.rainwood.water_authority.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.android.zxing.android.QRCodeCaptureActivity;
import com.rainwood.water_authority.R;
import com.rainwood.water_authority.aop.SingleClick;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.other.IntentKey;
import com.rainwood.water_authority.util.Constants;
import com.rainwood.water_authority.util.LogUtils;
import com.sxs.tools.permission.OnPermission;
import com.sxs.tools.permission.Permission;
import com.sxs.tools.permission.XXPermissions;

import java.util.List;

/**
 * @author sxs
 * @date :2020/11/10
 * @desc: 错误页面
 */
public class ErrorPageAty extends MyBaseActivity {

    public static void start(Context context, String title){
        Intent intent = new Intent(context, ErrorPageAty.class);
        intent.putExtra(IntentKey.TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_error_page;
    }

    @Override
    protected void initView() {
        setOnClickListener(R.id.btn_scan_again);
    }

    @Override
    protected void initData() {
        setLeftTitle(getString(IntentKey.TITLE));
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_scan_again){
            XXPermissions.with(this)
                    .permission(Permission.CAMERA)
                    .permission(Permission.Group.STORAGE)
                    .request(new OnPermission() {
                        @Override
                        public void hasPermission(List<String> granted, boolean all) {
                            if (all) {
                                //跳转扫码页面
                                Intent intent = new Intent(getContext(), QRCodeCaptureActivity.class);
                                intent.putExtra(QRCodeCaptureActivity.STATUS_BAR_COLOR, Color.parseColor("#99000000"));
                                startActivityForResult(intent, Constants.REQUEST_SCAN_CODE);
                            }
                        }

                        @Override
                        public void noPermission(List<String> denied, boolean quick) {
                            if (quick) {
                                toast(R.string.common_permission_fail);
                                XXPermissions.startPermissionActivity(getActivity(), false);
                            } else {
                                toast(R.string.common_permission_hint);
                            }
                        }
                    });
        }
    }

    //获取扫码结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_SCAN_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = null;
            if (bundle != null) {
                result = bundle.getString(QRCodeCaptureActivity.CODE_CONTENT);
                LogUtils.d(TAG, "-------- result-------- " + result);
            }
            if (TextUtils.isEmpty(result)) {
                toast("未识别到任何内容");
                return;
            }
            // TODO: 请求接口，拿到接口数据
            if (getString(IntentKey.TITLE).contains("盘点")){
                PanDianDetilsAct.start(this, result, "scan");
            }else if (getString(IntentKey.TITLE).contains("出库")){
                IssueOutboundAty.start(this, result, Constants.MATERIAL_ID);
            }else if (getString(IntentKey.TITLE).contains("入库")){
                ShouLiaoListAct.start(this, result);
            }
            finish();
        }
    }
}
